package com.ruoyi.model.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * @program: ruoyi
 * @description: Mysql数据源配置
 * @author: tangyongbo
 * @create: 2021-05-26 23:17
 **/
@Configuration
@MapperScan(basePackages = "com.ruoyi.model.mapper", sqlSessionTemplateRef = "mySqlSqlSessionTemplate" ,sqlSessionFactoryRef = "mySqlSqlSessionFactory")
public class MySqlDataSourceConfig {

        @Primary
        @Bean(name = "mySqlDataSource")
        @ConfigurationProperties(prefix = "mysql.datasource.druid")
        public DruidDataSource mySqlDataSource() {
            return DruidDataSourceBuilder.create().build();
        }

        @Primary
        @Bean(name = "mySqlTransactionManager")
        public DataSourceTransactionManager mySqlTransactionManager(@Qualifier("mySqlDataSource") DruidDataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }


        @Bean
        @ConfigurationProperties(prefix = "mybatis.configuration")//需要和yml中的mybatis配置对应
        public org.apache.ibatis.session.Configuration configuration(){
            return new org.apache.ibatis.session.Configuration();
        }

        @Primary
        @Bean(name = "mySqlSqlSessionFactory")
        public SqlSessionFactory mySqlSqlSessionFactory(@Qualifier("mySqlDataSource") DruidDataSource dataSource, org.apache.ibatis.session.Configuration configuration) throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setConfiguration(configuration);
            bean.setDataSource(dataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*/*.xml"));
            return bean.getObject();
        }

        @Bean(name = "mySqlSqlSessionTemplate")
        @Primary
        public SqlSessionTemplate mySqlSqlSessionTemplate(@Qualifier("mySqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
            return new SqlSessionTemplate(sqlSessionFactory);
        }
}
